<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>工资想过万，键盘要敲烂</title>
    <style>
      body {
        font-size: 40px;
      }
    </style>
  </head>

  <body>
    <script>
      // replace
      // 参数1：可以是字符串，也可以是正则
      // 参数2：可以是字符串，也可以是函数(需要有返回值)
      let str = '谁是谁，你是谁'
      str = str.replace(/谁/g, '<span style="color: hotpink;">谁</span>')
      document.body.innerHTML = str

      let str2 = 'aOaohbAiocjeao'
      str2 = str2.replace(
        /a|o/gi,
        (item) => `<span style="color: hotpink;">${item}</span>`
      )
      document.body.innerHTML = str2
      let str3 = 'abcdgdc'
      str3 = str3.replace(
        /a|c/gi,
        (item) => `<span style="color: hotpink;">${item.toUpperCase()}</span>`
      )
      document.body.innerHTML = str3

      // 定义正则两个方式
      // 1./a/
      // 2.构造函数来定义正则RegExp
      // new RegExp('a')==>/a/
      // new RegExp('a','gi')==>/a/gi
      function highLight(str, keyword) {
        const reg = new RegExp(keyword, 'gi')
        return str.replace(
          reg,
          (item) => `<span style="color: hotpink;">${item.toUpperCase()}</span>`
        )
      }
      document.body.innerHTML = highLight('asbhv', 'a')
      console.log(highLight('asbhv', 'a'))
    </script>
  </body>
</html>
